package lc.栈;

import java.util.Stack;

/**
 * @description: 有效的括号
 * @author: qian
 * @createDate: 2021/9/30
 */
public class Math20 {

    public boolean isValid(String s) {

        Stack<Character> stack = new Stack<>();

        for (char c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else { // 右括号
                if (stack.isEmpty()) return false;
                Character pop = stack.pop();
                if (pop == '(' && c != ')') return false;
                if (pop == '[' && c != ']') return false;
                if (pop == '{' && c != '}') return false;
            }
        }
        return stack.isEmpty();
    }
}
